/*
* @(#)ListUserRoleAction.java
*
* Copyright (c) 2003 DCIVision Ltd
* All rights reserved.
*
* This software is the confidential and proprietary information of DCIVision
* Ltd ("Confidential Information").  You shall not disclose such Confidential
* Information and shall use it only in accordance with the terms of the license
* agreement you entered into with DCIVision Ltd.
*/
package com.dcivision.user.web;

import java.sql.Connection;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.dao.DmsObjectHomeFolderDAObject;
import com.dcivision.dms.dao.DmsPersonalStorageSetupDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.SystemFunctionConstant;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractListAction;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.user.bean.UserMember;
import com.dcivision.user.bean.UserRole;
import com.dcivision.user.dao.MtmUserGroupUserRoleDAObject;
import com.dcivision.user.dao.MtmUserRecordUserRoleDAObject;
import com.dcivision.user.dao.UserRoleDAObject;
import com.dcivision.workflow.dao.MtmWorkflowDynamicUserActorDAObject;
import com.dcivision.workflow.dao.MtmWorkflowStepUserActorDAObject;

/**
 * <p>Class Name:       ListUserRoleAction.java    </p>
 * <p>Description:      The list action class for ListUserRole.jsp</p>
 *
 *    @author           Phoebe Wong
 *    @company          DCIVision Limited
 *    @creation date    24/07/2003
 *    @version          $Revision: 1.7 $
 */

public class ListUserRoleAction extends AbstractListAction {

  public static final String REVISION = "$Revision: 1.7 $";


  /**
   *  Constructor - Creates a new instance of ListUserRoleAction and define the default listName.
   */
  public ListUserRoleAction() {
    super();
    this.setListName("userRoleList");
  }

  /**
   * getMajorDAOClassName
   *
   * @return  The class name of the major DAObject will be used in this action.
   */
  public String getMajorDAOClassName() {
    return("com.dcivision.user.dao.UserRoleDAObject");
  }

  /**
   * getFunctionCode
   *
   * @return  The corresponding system function code of action.
   */
  public String getFunctionCode() {
    return(SystemFunctionConstant.SETUP_USER_ROLE);
  }
  
  /**
   *  Method deleteListData() - delete the item(s) from listing page
   *
   *  @param      mapping               ActionMapping from struts
   *  @param      form                  ActionForm from struts
   *  @param      request               HttpServletReuqest
   *  @param      respond               HttpServletRespond
   *  @return     void
   *  @throws     ApplicationException  Throws ApplicationException the deleteion failed
   */
  public void deleteListData(ActionMapping mapping, AbstractSearchForm form, HttpServletRequest request, HttpServletResponse response) throws ApplicationException {
    //simon_add_S
	SessionContainer sessionContainer=this.getSessionContainer(request);
	Connection conn=this.getConnection(request);
	String[] basicSelectID = form.getBasicSelectedID();
	
	if (!Utility.isEmpty(basicSelectID)){
		UserRoleDAObject userRoleDAO=new UserRoleDAObject(sessionContainer,conn);
		MtmWorkflowStepUserActorDAObject mtmWorkflowStepActorDAO=new MtmWorkflowStepUserActorDAObject(sessionContainer,conn);
		MtmWorkflowDynamicUserActorDAObject dynamicUserActorDAO=new MtmWorkflowDynamicUserActorDAObject(sessionContainer,conn);
		String[] tmpBasicSelectID = new String[basicSelectID.length];
  		int tmpBasicSelectIndex = 0;
  		
		for(int i=0;i<basicSelectID.length;i++){
			if(Utility.isEmpty(basicSelectID[i])){
				continue;
			}
			List userRoleList=mtmWorkflowStepActorDAO.getListWorkflowStepIDByActorIDAndActorType(TextUtility.parseIntegerObj(basicSelectID[i]),UserMember.MEMBER_TYPE_ROLE);
  			List dynamicUserList=dynamicUserActorDAO.getListWorkflowStepIDByActorIDAndActorType(TextUtility.parseIntegerObj(basicSelectID[i]),UserMember.MEMBER_TYPE_ROLE);			
			if(Utility.isEmpty(userRoleList) && Utility.isEmpty(dynamicUserList)){
				tmpBasicSelectID[tmpBasicSelectIndex] = basicSelectID[i];
  				++tmpBasicSelectIndex;
			} else {
  				Integer userRoelRecordID=TextUtility.parseIntegerObj(basicSelectID[i]);
  				UserRole userRoleRecord = (UserRole)userRoleDAO.getObjectByID(userRoelRecordID);
  				this.addError(request, "common.message.records_not_delete", userRoleRecord.getRoleName());
  			}
		}
		
		if (tmpBasicSelectIndex > 0){
  			String[] tmp = new String[tmpBasicSelectIndex];
  			for (int m = 0; m < tmpBasicSelectIndex; m++){
    			tmp[m] = tmpBasicSelectID[m]; 
    			form.setBasicSelectedID(tmp);
    		}
  		} else {
  			form.setBasicSelectedID(null);
  		}
	}
	//simon_add_E
	if (!Utility.isEmpty(form.getBasicSelectedID())) {//simon_add
		super.deleteListData(mapping,form,request,response);
	    //SessionContainer sessionContainer=this.getSessionContainer(request);//simon_del
	    //Connection conn=this.getConnection(request);//simon_del
	    DmsObjectHomeFolderDAObject dmsObjectHomeFolderDAO=new DmsObjectHomeFolderDAObject(sessionContainer,conn);
	    DmsPersonalStorageSetupDAObject dmsPersonalStorageSetupDAO=new DmsPersonalStorageSetupDAObject(sessionContainer,conn);
	    for(int i=0;i<form.getBasicSelectedID().length;i++){
	      Integer roleID=TextUtility.parseIntegerObj(form.getBasicSelectedID()[i]);
	      dmsObjectHomeFolderDAO.deleteByMemberIDMemberType(roleID,UserMember.MEMBER_TYPE_ROLE);
	      dmsPersonalStorageSetupDAO.deleteByMemberIDMemberType(roleID,UserMember.MEMBER_TYPE_ROLE);
	
	      MtmUserGroupUserRoleDAObject mtmUserGroupUserRoleDAO = new MtmUserGroupUserRoleDAObject(this.getSessionContainer(request), this.getConnection(request));
	      // Delete the old user roles belonged to the user group
	      mtmUserGroupUserRoleDAO.deleteListByUserRoleID(roleID);
	
	      MtmUserRecordUserRoleDAObject mtmUserRecordUserRoleDAO = new MtmUserRecordUserRoleDAObject(this.getSessionContainer(request), this.getConnection(request));
	      // Delete the old user groups belonged to the user
	      mtmUserRecordUserRoleDAO.deleteListByUserRoleID(roleID);
	    }
	}//simon_add
  }
}
